﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;

public partial class knowledge : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;

    //產品知識分類NodeId=5
    private int m_ParentNodeId = 5;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();

        if (!IsPostBack)
        {
            initSelect();
            fillRepeater();
        }
    }

    private void initSelect()
    {
        ddlSelect.Items.Clear();
        IList<NodeVO> subNodeList = m_PostService.GetNodeListByParentId(m_ParentNodeId);
        ddlSelect.Items.Add(new ListItem("全部", ""));
        if (subNodeList != null && subNodeList.Count > 0)
        {
            foreach (NodeVO nodeVO in subNodeList)
            {
                ddlSelect.Items.Add(new ListItem(nodeVO.Name, nodeVO.NodeId.ToString()));
            }
        }
    }

    private void fillRepeater()
    {        
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("Flag", "1");

        string nodeId = ddlSelect.SelectedValue;
        if (string.IsNullOrEmpty(nodeId))
        {
            conditions.Add("ParentNodeId", m_ParentNodeId.ToString());
        }
        else
        {
            conditions.Add("NodeId", nodeId);
        }

        //分頁
        AspNetPager1.RecordCount = m_PostService.GetPostCount(conditions);
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;

        conditions.Add("PageIndex", pageIndex.ToString());
        conditions.Add("PageSize", pageSize.ToString());

        Repeater1.DataSource = m_PostService.GetPostList(conditions);
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        fillRepeater();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;

            //內容
            int titleLength = 40;
            if (!string.IsNullOrEmpty(postVO.Title))
            {
                string title = postVO.Title;
                if (title.Length > titleLength)
                {
                    title = title.Substring(0, titleLength) + "...";
                }

                UIHelper.SetLabelText(ref ctrl, "lblTitle", title);
            }
        }
    }

    protected void ddlSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillRepeater();
    }
}